﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AssetAllocationSystem.Models;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class RequestRepository : RepositoryBase<Request>
    {
        private ISession session;
        public RequestRepository(ISession session)
            : base(session)
        {
            this.session = session;
        }

        public Request FindById(Guid id)
        {
            var request = DetachedCriteria
                .For<Request>()
                .Add(Restrictions.Eq("Id", id))
                .GetExecutableCriteria(session)
                .UniqueResult<Request>();

            return request;
        }

        public IList<Request> FindByEmployeeId(Guid id)
        {
            var request = DetachedCriteria
                .For<Request>()
                .CreateAlias("Employee", "emp")
                .Add(Restrictions.Eq("emp.Id", id))
                .GetExecutableCriteria(session)
                .List<Request>();

            return request;
        }

        public IList<Request> FindByEmployeeName(string name)
        {
            var request = DetachedCriteria
                .For<Request>()
                .CreateAlias("Employee", "emp")
                .Add(Restrictions.Eq("emp.Name", name))
                .GetExecutableCriteria(session)
                .List<Request>();

            return request;
        }

    }
}